package com.fingard.dsp.bank.directbank.chinapay03;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.fingard.dsp.bank.directConfig.FtpSet;
import com.fingard.dsp.bank.directConfig.TaskTimerSet;
import com.fingard.dsp.bank.directbank.bankInterface.IScheduleTask;
import com.fingard.net.FtpPortClient;

public class ChinaPay03ScheduleTask extends ChinaPay03Base implements IScheduleTask {
    @Override
    public void execTask(TaskTimerSet timerSet) {
        if (timerSet.taskNameList.toLowerCase().contains("statement")) {
            getStatement();
        }
    }

    private void getStatement() {
        try {
            FtpSet ftpSet = bankFront.getFtpSet("statement");
            WriteBankLogLn("FTP下载地址" + ftpSet.serverIP + ":" + ftpSet.port);
            FtpPortClient ftpPtClient = new FtpPortClient(ftpSet);
            ftpPtClient.connect();
            if("康宁医院".equals(getBankConfig().getValueAsString("groupName"))){
                ftpPtClient.list(ftpSet.downDir, "_68.xls");
                ftpPtClient.list(ftpSet.downDir, "_1.xls");
                ftpPtClient.list(ftpSet.downDir, "_9999.xls");
                ftpPtClient.list(ftpSet.downDir, "_86.xls");
            }else {
                ftpPtClient.list(ftpSet.downDir, "_68.xls");
                ftpPtClient.list(ftpSet.downDir, "_47.xls");
                if("东航物流".equals(getBankConfig().getValueAsString("groupName"))) {
                    ftpPtClient.list(ftpSet.downDir, "_1.xls");
                    ftpPtClient.list(ftpSet.downDir, "_9.xls");
                }else if("海明控股".equals(getBankConfig().getValueAsString("groupName"))) {
                    ftpPtClient.list(ftpSet.downDir, "_1.xls");
                }
            }
            HashMap<String, String> downloadFiles = getNeedDownloadFiles(ftpPtClient.arFiles,list(statementFilePath));
            Iterator iter = downloadFiles.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                String ftpFilePath = entry.getKey().toString();
                String localFilePath = entry.getValue().toString();
                ftpPtClient.downloadFile(ftpFilePath, localFilePath);
                WriteBankLogLn("已下载文件" + ftpFilePath + "至" + localFilePath);
            }
            ftpPtClient.disconnect();
        } catch (Exception e) {
            WriteBankLogLn(e);
        }
    }

    ArrayList<String> arFiles = new ArrayList<String>();
    private ArrayList<String> list(String p_filePath) {
        File file = new File(p_filePath);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (files.length != 0) {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                        list(file2.getAbsolutePath());
                    } else {
                        arFiles.add(file2.getAbsolutePath());
                    }
                }
            }
        }
        return arFiles;
    }

    private HashMap<String, String> getNeedDownloadFiles(ArrayList<String> p_ftpFiles, ArrayList<String> p_localFiles) {
        HashMap<String, String> arFiles = new HashMap<String, String>();
        String downloadLimitDate = getBankConfig().getValueAsString("downloadLimitDate");
        for (String file : p_ftpFiles) {
            String fileName = file.substring(file.lastIndexOf("/") + 1);
            String fileDate = fileName.substring(fileName.indexOf("_") + 1, fileName.lastIndexOf("_"));
            if (fileDate.compareTo(downloadLimitDate) >= 0) {
                String localFileName = statementFilePath + fileDate + File.separator + fileName;
                if (!p_localFiles.contains(localFileName)) {
                    arFiles.put(file, localFileName);
                }
            }
        }
        return arFiles;
    }
}
